#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import jsonify, request
from flask_restful import Resource
from utility.odoo_manager import OdooManager  # 操作odoo的单例
from utility.error_code import error_code  # 异常代码
from utility.message_manager import MessageBusManager
from event import event_type
from command import command
from utility import tools
import logging

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)

class DeliveryRecAddAPI(Resource):
    """
    新增分娩记录单
    :request: post
    :param archivesId: 档案ID
    :param cfqk: 产妇情况
            {
                "yzZ": 孕周周
                "yzT": 孕周天
                "yc": 孕次
                "cc": 产次
                "cb": 产别
                "hbz": 合并症
                "hbzQt": 合并症其他
                "bfz": 并发症
                "sfccsyc": 是否催产素引产
                "sfwzycf": 是否危重孕产妇
            }
    :param dycc: 第一产程
            {
                "glgsksTime": 规律宫缩开始时间
                "tmpl": 胎膜破裂
                "pmsj": 破膜时间
                "gkqksj": 宫口全开时间
                "ycqk": 异常情况
                "cl": 处理
            }
    :param decc: 第二产程
            {
                "ts": 胎数
                "te": 胎儿
                    [
                        {
                            "id": id
                            "temcsj": 胎儿娩出时间
                            "tfw": 胎方位
                            "zt": 状态
                            "sex": 性别
                            "weigth": 体重
                            "length": 身长
                        }
                    ]
                "fmmzyw": 分娩麻醉用药
                "fmmzywQt": 分娩麻醉用药其他
                "fmfs": 分娩方式
                "fmfsQt": 分娩方式其他
                "gjtc": 宫颈探查
                "gjtcLs": 宫颈探查裂伤长度
                "hysfwz": 会阴是否完整
                "hylsqk": 会阴裂伤情况
                "sfhycq": 是否会阴侧切
                "hycqwz": 会阴侧切位置  正中 左斜 右斜
                "hycqsyz": 会阴侧切适应症
                "hycqmzfs": 会阴侧切麻醉方式
            }
    :param dscc: 第三产程
            {
                "te": 胎儿
                [
                    {
                        "tpmcsj": 胎盘娩出时间
                        "tpmcfs": 胎盘娩出方式
                        "tpsfwz": 胎盘是否完整
                        "tpsfsq": 是否手取
                        "tpqchsfwz": 取出后是否完整
                        "tpWeigth": 胎盘重量
                        "tpVolume": 胎盘体积
                        "tpGauge": 胎盘厚度
                        "tmsfwz": 胎膜是否完整
                        "tmsfsq": 胎膜是否手取
                        "tmqchsfwz": 胎膜取出后是否完整
                        "maxDistance": 最大距离
                        "minDistance": 最小距离
                        "qdLength": 脐带长度
                        "qdfz": 脐带附着
                        "qrjzs": 脐绕颈周数
                        "sfchyy": 是否产后用药
                        "chyyyl": 产后用药用量
                    }
                ],
                "ts": 胎数
                "fysgsjl": 服用缩宫素剂量
                "fysgs": 是否服用缩宫素
            }
    :param zz: 总则
            {
                "ysqk": 羊水情况
                "ysl": 羊水量
                "dyccSj": 第一产程时间分钟
                "deccSj": 第二产程时间分钟
                "dsccSj": 第三产程时间分钟
                "zccSj": 总产程时间毫秒
                "jszCode": 接生者编码
                "jszName": 接生者姓名
            }
    :param cfscqk: 产妇情况
            {
                "ccfsj": 出产房时间
                "ycfsfsw": 孕妇是否死亡
                "ycfsyzd": 孕产妇死因诊断
                "hyfhfs": 会阴缝合方式  外缝 皮内
                "hywfzs": 会阴外缝针数
                "cxl": 出血量
                "cxl1xs": 1小时出血量
                "cxl2xs": 2小时出血量
                "xyChSsq": 产后血压收缩期
                "xyChSzq": 产后血压舒张期
                "xyCh1xsSsq": 产后1小时血压收缩期
                "xyCh1xsSzq": 产后1小时血压舒张缩期
                "xyCh2xsSsq": 产后2小时血压收缩期
                "xyCh2xsSzq": 产后2小时血压舒张缩期
                "mbCh": 产后脉搏
                "mbCh1xs": 产后1小时脉搏
                "tbqk": 特别情况
                "cstbjl": 产时特别记录
            }
    :returns: json string
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def post(self):
        try:
            
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId','cfqk','dycc','decc','dscc','zz','cfscqk']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 新增分娩记录单
            data_ = {
                'archives_id': params.get('archivesId'),
                'cf_yz_week': tools.nullablea_dict(params.get('cfqk'), 'yzZ'),
                'cf_yz_day': tools.nullablea_dict(params.get('cfqk'), 'yzT'),
                'cf_yc': tools.nullablea_dict(params.get('cfqk'), 'yc'),
                'cf_ts': params.get('ts'),
                'cf_cb': tools.nullablea_dict(params.get('cfqk'), 'cb'),
                'cf_hbz': tools.nullablea_dict(params.get('cfqk'), 'hbz'),
                'cf_hbz_other': tools.nullablea_dict(params.get('cfqk'), 'hbzQt'),
                'cf_bfz': tools.nullablea_dict(params.get('cfqk'), 'bfz'),
                'cf_ccsyc': tools.nullablea_dict(params.get('cfqk'), 'sfccsyc'),
                'cf_wzycf': tools.nullablea_dict(params.get('cfqk'), 'sfwzycf'),

                'dycc_gs_time': tools.nullablea_dict(params.get('dycc'), 'glgsksTime'),
                'dycc_tmpl': tools.nullablea_dict(params.get('dycc'), 'tmpl'),
                'dycc_dm_time': tools.nullablea_dict(params.get('dycc'), 'pmsj'),
                'dycc_gkqk_time': tools.nullablea_dict(params.get('dycc'), 'gkqksj'),
                'dycc_ycqk': tools.nullablea_dict(params.get('dycc'), 'ycqk'),
                'dycc_ycqk_cl': tools.nullablea_dict(params.get('dycc'), 'cl'),
                'decc_fmmz_yy': tools.nullablea_dict(params.get('decc'), 'fmmzyw'),
                'decc_fmmz_yy_qt': tools.nullablea_dict(params.get('decc'), 'fmmzywQt'),
                'decc_fmmz_yy_jl': tools.nullablea_dict(params.get('decc'), 'deccFmmzYyJl'),
                'decc_fmfs': tools.nullablea_dict(params.get('decc'), 'fmfs'),
                'decc_fmfs_qt': tools.nullablea_dict(params.get('decc'), 'fmfsQt'),
                # 宫颈探查
                'decc_gj': tools.nullablea_dict(params.get('decc'), 'gjtc'),
                # 宫颈裂伤情况
                'decc_gj_tc': tools.nullablea_dict(params.get('decc'), 'gjtcqk'),
                #  宫颈裂伤长度
                'decc_gj_qk': tools.nullablea_dict(params.get('decc'), 'gjtcLs'),
                'decc_hy': tools.nullablea_dict(params.get('decc'), 'hysfwz'),
                'decc_hy_ls': tools.nullablea_dict(params.get('decc'), 'hylsqk'),
                'decc_hy_cq_wz': tools.nullablea_dict(params.get('decc'), 'hycqwz'),
                'decc_hy_cq': tools.nullablea_dict(params.get('decc'), 'sfhycq'),
                'decc_hy_cq_syz': tools.nullablea_dict(params.get('decc'), 'hycqsyz'),
                'decc_hy_cq_mzfs': tools.nullablea_dict(params.get('decc'), 'hycqmzfs'),

                'cf_chyy': tools.nullablea_dict(params.get('dscc'), 'fysgs'),
                'cf_chyyjl': tools.nullablea_dict(params.get('dscc'), 'fysgsjl'),

                'zz_ysqk': tools.nullablea_dict(params.get('zz'), 'ysqk'),
                'zz_ysl': tools.nullablea_dict(params.get('zz'), 'ysl'),
                'zz_jsz': tools.nullablea_dict(params.get('zz'), 'jszCode'),
                'cf_ycfsw': tools.nullablea_dict(params.get('cfscqk'), 'ycfsfsw'),
                'cf_ycfswzd': tools.nullablea_dict(params.get('cfscqk'), 'ycfsyzd'),
                'cf_hy_fz': tools.nullablea_dict(params.get('cfscqk'), 'hyfhfs'),
                'cf_hy_fz_s': tools.nullablea_dict(params.get('cfscqk'), 'hywfzs'),
                'cf_ccfsj': tools.nullablea_dict(params.get('cfscqk'), 'ccfsj'),
                'cf_cx': tools.nullablea_dict(params.get('cfscqk'), 'cxl'),
                'cf_yxs': tools.nullablea_dict(params.get('cfscqk'), 'cxl1xs'),
                'cf_exs': tools.nullablea_dict(params.get('cfscqk'), 'cxl2xs'),
                'cf_xych_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyChSzq'),
                'cf_xych_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyChSsq'),
                'cf_xychyss_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh1xsSzq'),
                'cf_xychyss_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh1xsSsq'),
                'cf_xychlss_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh2xsSzq'),
                'cf_xychlss_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh2xsSsq'),
                'cf_xymb': tools.nullablea_dict(params.get('cfscqk'), 'mbCh'),
                'cf_xymbyss': tools.nullablea_dict(params.get('cfscqk'), 'mbCh1xs'),
                'cf_tbqk': tools.nullablea_dict(params.get('cfscqk'), 'tbqk'),
                'cf_cstbjl': tools.nullablea_dict(params.get('cfscqk'), 'cstbjl'),
                # 时长为秒数
                'zz_dycc_sc': 0,
                'zz_decc_sc': 0,
                'zz_dscc_sc': 0,
                'hospital_id': 1
            }

            # 计算第一产程
            data_['zz_dycc_sc'] = tools.diff_datetime_count(data_['dycc_gs_time'], data_['dycc_gkqk_time'])
            # 计算第二产程
            data_['zz_decc_sc'] = tools.diff_datetime_count(data_['dycc_gkqk_time'], params.get('te')[0]['temcsj'])
            # 计算第三产程
            data_['zz_dscc_sc'] = 0
            # 插入 one2many字段
            tes = params.get('te')
            o2m_data = []
            if tes:
                for te in tes:
                    o2m_data.append((0, 0, {
                        'decc_tpmc_time': te['temcsj'],
                        'decc_tfw': te['tfw'],
                        "decc_tezt": te['zt'],
                        "decc_texb": te['xb'],
                        'decc_tetz': te['weight'],
                        'decc_tesc': te['length'],
                        'dscc_tpmc_time': te['tpmcsj'],
                        'dscc_tpmc_fs': te['tpmcfs'],
                        'dscc_tp': te['tpsfwz'],
                        'dscc_tp_sq': te['tpsfsq'],
                        'dscc_tp_sqh': te['tpqchsfwz'],
                        'dscc_tp_zl': te['tpWeigth'],
                        'dscc_tp_tj': te['tpVolume'],
                        'dscc_tp_hd': te['tpGauge'],
                        'dscc_tm': te['tmsfwz'],
                        'dscc_tm_sq': te['tmsfsq'],
                        'dscc_tm_sqh': te['tmqchsfwz'],
                        'dscc_tm_zdjl': te['maxDistance'],
                        'dscc_tm_zxjl': te['minDistance'],
                        'dscc_qdcd': te['qdLength'],
                        'dscc_fz': te['qdfz'],
                        'dscc_qdrj': te['qrjzs']
                    }))
            data_['record_line'] = o2m_data
            data_ = odoo.env['birth.record'].create(data_)
            create_event = event_type.OperationLogEvent('新增分娩记录单', 'id:{}'.format(data_))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': data_}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class DeliveryRecUpdateAPI(Resource):
    """
    修改分娩记录单
    :request: put
    :param archivesId: 档案ID
    :param cfqk: 产妇情况
            {
                "yzZ": 孕周周
                "yzT": 孕周天
                "yc": 孕次
                "cc": 产次
                "cb": 产别
                "hbz": 合并症
                "hbzQt": 合并症其他
                "bfz": 并发症
                "sfccsyc": 是否催产素引产
                "sfwzycf": 是否危重孕产妇
            }
    :param dycc: 第一产程
            {
                "glgsksTime": 规律宫缩开始时间
                "tmpl": 胎膜破裂
                "pmsj": 破膜时间
                "gkqksj": 宫口全开时间
                "ycqk": 异常情况
                "cl": 处理
            }
    :param decc: 第二产程
            {
                "ts": 胎数
                "te": 胎儿
                    [
                        {
                            "id": id
                            "temcsj": 胎儿娩出时间
                            "tfw": 胎方位
                            "zt": 状态
                            "sex": 性别
                            "weigth": 体重
                            "length": 身长
                        }
                    ]
                "fmmzyw": 分娩麻醉用药
                "fmmzywQt": 分娩麻醉用药其他
                "fmfs": 分娩方式
                "fmfsQt": 分娩方式其他
                "gjtc": 宫颈探查
                "gjtcLs": 宫颈探查裂伤长度
                "hysfwz": 会阴是否完整
                "hylsqk": 会阴裂伤情况
                "sfhycq": 是否会阴侧切
                "hycqwz": 会阴侧切位置  正中 左斜 右斜
                "hycqsyz": 会阴侧切适应症
                "hycqmzfs": 会阴侧切麻醉方式
            }
    :param dscc: 第三产程
            {
                "te": 胎儿
                [
                    {
                        "tpmcsj": 胎盘娩出时间
                        "tpmcfs": 胎盘娩出方式
                        "tpsfwz": 胎盘是否完整
                        "tpsfsq": 是否手取
                        "tpqchsfwz": 取出后是否完整
                        "tpWeigth": 胎盘重量
                        "tpVolume": 胎盘体积
                        "tpGauge": 胎盘厚度
                        "tmsfwz": 胎膜是否完整
                        "tmsfsq": 胎膜是否手取
                        "tmqchsfwz": 胎膜取出后是否完整
                        "maxDistance": 最大距离
                        "minDistance": 最小距离
                        "qdLength": 脐带长度
                        "qdfz": 脐带附着
                        "qrjzs": 脐绕颈周数
                        "sfchyy": 是否产后用药
                        "chyyyl": 产后用药用量
                    }
                ],
                "ts": 胎数
                "fysgsjl": 服用缩宫素剂量
                "fysgs": 是否服用缩宫素
            }
    :param zz: 总则
            {
                "ysqk": 羊水情况
                "ysl": 羊水量
                "dyccSj": 第一产程时间分钟
                "deccSj": 第二产程时间分钟
                "dsccSj": 第三产程时间分钟
                "zccSj": 总产程时间毫秒
                "jszCode": 接生者编码
                "jszName": 接生者姓名
            }
    :param cfscqk: 产妇情况
            {
                "ccfsj": 出产房时间
                "ycfsfsw": 孕妇是否死亡
                "ycfsyzd": 孕产妇死因诊断
                "hyfhfs": 会阴缝合方式  外缝 皮内
                "hywfzs": 会阴外缝针数
                "cxl": 出血量
                "cxl1xs": 1小时出血量
                "cxl2xs": 2小时出血量
                "xyChSsq": 产后血压收缩期
                "xyChSzq": 产后血压舒张期
                "xyCh1xsSsq": 产后1小时血压收缩期
                "xyCh1xsSzq": 产后1小时血压舒张缩期
                "xyCh2xsSsq": 产后2小时血压收缩期
                "xyCh2xsSzq": 产后2小时血压舒张缩期
                "mbCh": 产后脉搏
                "mbCh1xs": 产后1小时脉搏
                "tbqk": 特别情况
                "cstbjl": 产时特别记录
            }
    :returns: json strings
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def put(self):
        try:
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['archivesId','cfqk','dycc','decc','dscc','zz','cfscqk', 'id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [('archives_id', '=', int(params.get('archivesId'))), ('id', '=', int(params.get('id')))]
            birth_record = odoo.env['birth.record'].search_read(query_)
            if not birth_record:
                return jsonify({'code': 40005, 'msg': error_code[40005]})
            birth_record = birth_record.pop()
            birth_record_id = birth_record['id']
            # 修改分娩记录单
            data_ = {
                'archives_id': params.get('archivesId'),
                'cf_yz_week': tools.nullablea_dict(params.get('cfqk'), 'yzZ'),
                'cf_yz_day': tools.nullablea_dict(params.get('cfqk'), 'yzT'),
                'cf_yc': tools.nullablea_dict(params.get('cfqk'), 'yc'),
                'cf_ts': params.get('ts'),
                'cf_cb': tools.nullablea_dict(params.get('cfqk'), 'cb'),
                'cf_hbz': tools.nullablea_dict(params.get('cfqk'), 'hbz'),
                'cf_hbz_other': tools.nullablea_dict(params.get('cfqk'), 'hbzQt'),
                'cf_bfz': tools.nullablea_dict(params.get('cfqk'), 'bfz'),
                'cf_ccsyc': tools.nullablea_dict(params.get('cfqk'), 'sfccsyc'),
                'cf_wzycf': tools.nullablea_dict(params.get('cfqk'), 'sfwzycf'),

                'dycc_gs_time': tools.nullablea_dict(params.get('dycc'), 'glgsksTime'),
                'dycc_tmpl': tools.nullablea_dict(params.get('dycc'), 'tmpl'),
                'dycc_dm_time': tools.nullablea_dict(params.get('dycc'), 'pmsj'),
                'dycc_gkqk_time': tools.nullablea_dict(params.get('dycc'), 'gkqksj'),
                'dycc_ycqk': tools.nullablea_dict(params.get('dycc'), 'ycqk'),
                'dycc_ycqk_cl': tools.nullablea_dict(params.get('dycc'), 'cl'),
                'decc_fmmz_yy': tools.nullablea_dict(params.get('decc'), 'fmmzyw'),
                'decc_fmmz_yy_qt': tools.nullablea_dict(params.get('decc'), 'fmmzywQt'),
                'decc_fmmz_yy_jl': tools.nullablea_dict(params.get('decc'), 'deccFmmzYyJl'),
                'decc_fmfs': tools.nullablea_dict(params.get('decc'), 'fmfs'),
                'decc_fmfs_qt': tools.nullablea_dict(params.get('decc'), 'fmfsQt'),
                # 宫颈探查
                'decc_gj': tools.nullablea_dict(params.get('decc'), 'gjtc'),
                # 宫颈裂伤情况
                'decc_gj_tc': tools.nullablea_dict(params.get('decc'), 'gjtcqk'),
                #  宫颈裂伤长度
                'decc_gj_qk': tools.nullablea_dict(params.get('decc'), 'gjtcLs'),
                'decc_hy': tools.nullablea_dict(params.get('decc'), 'hysfwz'),
                'decc_hy_ls': tools.nullablea_dict(params.get('decc'), 'hylsqk'),
                'decc_hy_cq_wz': tools.nullablea_dict(params.get('decc'), 'hycqwz'),
                'decc_hy_cq': tools.nullablea_dict(params.get('decc'), 'sfhycq'),
                'decc_hy_cq_syz': tools.nullablea_dict(params.get('decc'), 'hycqsyz'),
                'decc_hy_cq_mzfs': tools.nullablea_dict(params.get('decc'), 'hycqmzfs'),

                'cf_chyy': tools.nullablea_dict(params.get('dscc'), 'fysgs'),
                'cf_chyyjl': tools.nullablea_dict(params.get('dscc'), 'fysgsjl'),

                'zz_ysqk': tools.nullablea_dict(params.get('zz'), 'ysqk'),
                'zz_ysl': tools.nullablea_dict(params.get('zz'), 'ysl'),
                'zz_jsz': tools.nullablea_dict(params.get('zz'), 'jszCode'),
                'cf_ycfsw': tools.nullablea_dict(params.get('cfscqk'), 'ycfsfsw'),
                'cf_ycfswzd': tools.nullablea_dict(params.get('cfscqk'), 'ycfsyzd'),
                'cf_hy_fz': tools.nullablea_dict(params.get('cfscqk'), 'hyfhfs'),
                'cf_hy_fz_s': tools.nullablea_dict(params.get('cfscqk'), 'hywfzs'),
                'cf_ccfsj': tools.nullablea_dict(params.get('cfscqk'), 'ccfsj'),
                'cf_cx': tools.nullablea_dict(params.get('cfscqk'), 'cxl'),
                'cf_yxs': tools.nullablea_dict(params.get('cfscqk'), 'cxl1xs'),
                'cf_exs': tools.nullablea_dict(params.get('cfscqk'), 'cxl2xs'),
                'cf_xych_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyChSzq'),
                'cf_xych_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyChSsq'),
                'cf_xychyss_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh1xsSzq'),
                'cf_xychyss_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh1xsSsq'),
                'cf_xychlss_szy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh2xsSzq'),
                'cf_xychlss_ssy': tools.nullablea_dict(params.get('cfscqk'), 'xyCh2xsSsq'),
                'cf_xymb': tools.nullablea_dict(params.get('cfscqk'), 'mbCh'),
                'cf_xymbyss': tools.nullablea_dict(params.get('cfscqk'), 'mbCh1xs'),
                'cf_tbqk': tools.nullablea_dict(params.get('cfscqk'), 'tbqk'),
                'cf_cstbjl': tools.nullablea_dict(params.get('cfscqk'), 'cstbjl'),
                # 时长为秒数
                'zz_dycc_sc': 0,
                'zz_decc_sc': 0,
                'zz_dscc_sc': 0,
                'hospital_id': 1
            }
            # 计算第一产程
            data_['zz_dycc_sc'] = tools.diff_datetime_count(data_['dycc_gs_time'], data_['dycc_gkqk_time'])
            # 计算第二产程
            data_['zz_decc_sc'] = tools.diff_datetime_count(data_['dycc_gkqk_time'], params.get('te')[0]['temcsj'])
            # 计算第三产程
            data_['zz_dscc_sc'] = 0
            # 插入 one2many字段
            # 先插入新生儿记录 获取id 然后做替换
            tes = params.get('te')
            ids = []
            for te in tes:
                id = odoo.env['birth.record.line'].create({
                    'main_id': int(birth_record_id),
                    'decc_tpmc_time': te['temcsj'],
                    'decc_tfw': te['tfw'],
                    "decc_tezt": te['zt'],
                    "decc_texb": te['xb'],
                    'decc_tetz': te['weight'],
                    'decc_tesc': te['length'],
                    'dscc_tpmc_time': te['tpmcsj'],
                    'dscc_tpmc_fs': te['tpmcfs'],
                    'dscc_tp': te['tpsfwz'],
                    'dscc_tp_sq': te['tpsfsq'],
                    'dscc_tp_sqh': te['tpqchsfwz'],
                    'dscc_tp_zl': te['tpWeigth'],
                    'dscc_tp_tj': te['tpVolume'],
                    'dscc_tp_hd': te['tpGauge'],
                    'dscc_tm': te['tmsfwz'],
                    'dscc_tm_sq': te['tmsfsq'],
                    'dscc_tm_sqh': te['tmqchsfwz'],
                    'dscc_tm_zdjl': te['maxDistance'],
                    'dscc_tm_zxjl': te['minDistance'],
                    'dscc_qdcd': te['qdLength'],
                    'dscc_fz': te['qdfz'],
                    'dscc_qdrj': te['qrjzs']
                })
                ids.append(id)
            # 删除原胎儿记录
            for birth_record_te in birth_record['record_line']:
                odoo.env['birth.record.line'].unlink(birth_record_te)
            odoo.env['birth.record'].write(birth_record_id, data_)
            create_event = event_type.OperationLogEvent('修改分娩记录单', 'id:{}'.format(params.get('id')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': birth_record_id}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class DeliveryRecListAPI(Resource):
    """
    回显分娩记录单
    :request: get
    :param archivesId: 档案ID
    :returns: json string
    """
    #@AppSecurity.public_verify()
    #@AppSecurity.token_verify()
    def get(self):
        try:
            # 参数校验
            params = request.values
            req_params = ['archivesId', 'id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            #  获取回显分娩记录单
            odoo = OdooManager().get_instance()
            #  构造查询条件
            query_ = [
                ('archives_id', '=', int(params.get('archivesId'))),
                ('id', '=', int(params.get('id')))
            ]
            birth_record = odoo.env['birth.record'].search_read(query_)
            data_ = {
                'id': int(params.get('id'))
            }
            if birth_record:
                birth_record = birth_record.pop()
                #  产妇情况
                data_['cfqk'] = {
                    'yzZ': birth_record['cf_yz_week'],
                    'yzT': birth_record['cf_yz_day'],
                    'yc': birth_record['cf_yc'],
                    'cc': birth_record['cf_ts'],
                    'cb': birth_record['cf_cb'],
                    'hbz': birth_record['cf_hbz'],
                    'hbzQt': birth_record['cf_hbz_other'],
                    'bfz': birth_record['cf_bfz'],
                    'sfccsyc': birth_record['cf_ccsyc'],
                    'sfwzycf': birth_record['cf_wzycf']
                }
                #  第一产程
                data_['dycc'] = {
                    'glgsksTime': birth_record['dycc_gs_time'],
                    'tmpl': birth_record['dycc_tmpl'],
                    'pmsj': birth_record['dycc_dm_time'],
                    'gkqksj': birth_record['dycc_gkqk_time'],
                    'ycqk': birth_record['dycc_ycqk'],
                    'cl': birth_record['dycc_ycqk_cl'],
                }
                #  第二产程
                data_['decc'] = {
                    'fmmzyw': birth_record['decc_fmmz_yy'],
                    'fmmzywQt': birth_record['decc_fmmz_yy_qt'],
                    'deccFmmzYyJl': birth_record['decc_fmmz_yy_jl'],
                    'fmfs': birth_record['decc_fmfs'],
                    'fmfsQt': birth_record['decc_fmfs_qt'],
                    'gjtc': birth_record['decc_gj'],
                    'gjtcqk': birth_record['decc_gj_tc'],
                    'gjtcLs': birth_record['decc_gj_qk'],
                    'hysfwz': birth_record['decc_hy'],
                    'hylsqk': birth_record['decc_hy_ls'],
                    'hycqwz': birth_record['decc_hy_cq_wz'],
                    'sfhycq': birth_record['decc_hy_cq'],
                    'hycqsyz': birth_record['decc_hy_cq_syz'],
                    'hycqmzfs': birth_record['decc_hy_cq_mzfs'],
                }
                #  第三产程
                data_['dscc'] = {
                    'fysgsjl': birth_record['cf_chyyjl'],
                    'fysgs': birth_record['cf_chyy']
                }
                #  总则
                data_['zz'] = {
                    'ysqk': birth_record['zz_ysqk'],
                    'ysl': birth_record['zz_ysl'],
                    'jszCode': birth_record['zz_jsz'][0] if birth_record['zz_jsz'] else None,
                }
                #  产妇情况
                data_['cfscqk'] = {
                    'ycfsfsw': birth_record['cf_ycfsw'],
                    'ycfsyzd': birth_record['cf_ycfswzd'],
                    'hyfhfs': birth_record['cf_hy_fz'],
                    'hywfzs': birth_record['cf_hy_fz_s'],
                    'ccfsj': birth_record['cf_ccfsj'],
                    'cxl': birth_record['cf_cx'],
                    'cxl1xs': birth_record['cf_yxs'],
                    'cxl2xs': birth_record['cf_exs'],
                    'xyChSzq': birth_record['cf_xych_szy'],
                    'xyChSsq': birth_record['cf_xych_ssy'],
                    'xyCh1xsSzq': birth_record['cf_xychyss_szy'],
                    'xyCh1xsSsq': birth_record['cf_xychyss_ssy'],
                    'xyCh2xsSzq': birth_record['cf_xychlss_szy'],
                    'xyCh2xsSsq': birth_record['cf_xychlss_ssy'],
                    'mbCh': birth_record['cf_xymb'],
                    'mbCh1xs': birth_record['cf_xymbyss'],
                    'tbqk': birth_record['cf_tbqk'],
                    'cstbjl': birth_record['cf_cstbjl']
                }
                # 胎儿数据
                data_['ts'] = birth_record['cf_ts']
                te = []
                tes = odoo.env['birth.record.line'].search_read([('id', 'in', birth_record['record_line'])])
                for te_item in tes:
                    te.append({
                        'id': te_item['id'],
                        'length': te_item['decc_tesc'],
                        'weight': te_item['decc_tetz'],
                        'xb': te_item['decc_texb'],
                        'zt': te_item['decc_tezt'],
                        'tfw': te_item['decc_tfw'],
                        'temcsj': te_item['decc_tpmc_time'],
                        'tpmcsj': te_item['dscc_tpmc_time'],
                        'tpmcfs': te_item['dscc_tpmc_fs'],
                        'tpsfwz': te_item['dscc_tp'],
                        'tpsfsq': te_item['dscc_tp_sq'],
                        'tpqchsfwz': te_item['dscc_tp_sqh'],
                        'tpWeigth': te_item['dscc_tp_zl'],
                        'tpVolume': te_item['dscc_tp_tj'],
                        'tpGauge': te_item['dscc_tp_hd'],
                        'qdLength': te_item['dscc_qdcd'],
                        'tmsfwz': te_item['dscc_tm'],
                        'tmsfsq': te_item['dscc_tm_sq'],
                        'qdfz': te_item['dscc_fz'],
                        'tmqchsfwz': te_item['dscc_tm_sqh'],
                        'qrjzs': te_item['dscc_qdrj'],
                        'maxDistance': te_item['dscc_tm_zdjl'],
                        'minDistance': te_item['dscc_tm_zxjl']
                    })
                data_['te'] = te
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

